package uva.asimov.asimovtrainner;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

/**
 * 
 * @author Víctor Alonso Egido
 * @author Edgar Guaza Mayo
 * @author Rubén Moya Vázquez
 *  
 * Muestra los detalles de un entrenamiento concreto. Incorpora los fragmentos
 * DetailFragment y GlobalFragment 
 */
public class DetailTrainActivity extends Activity {

	//private int ID;
	private Cursor cursor;
	private static final String ASIMOV = MainActivity.class.getSimpleName();
	
	private TextView txtShowTipo;
	private TextView txtShowDescripcion;
	private TextView txtShowFecha;
	private TextView txtShowDuracion;
	private TextView txtShowDistancia;
	private TextView txtShowVelocidad;
	private TextView txtShowOtros;
	private TextView txtShowUbicacion;
	
	
	/**
	 * Método de creación de la actividad 
	 * 
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail_train);
		Intent intent=getIntent();
		Bundle bundle =intent.getExtras();
		//ID= intent.getIntExtra("ELEMENTO_SELECCIONADO", -1);
		
		
		String ID=bundle.getString("ELEMENTO_SELECCIONADO");
		Log.d(ASIMOV,String.valueOf(ID)+"******************");
		rellenaCampos(ID);
	}



	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.detail_train, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	/**
	 * Rellena los campos con los valores de la base de datos
	 */
	private void rellenaCampos(String ID	) {
		DataBaseManager dm=new DataBaseManager(this);
		Log.d(ASIMOV,String.valueOf(ID)+"******************");
		//if (ID>=0){
			Log.d(ASIMOV,String.valueOf(ID)+"******************");
			
		cursor=dm.buscarEntrenamiento(ID);
		Log.d(ASIMOV,"-----Longitud cursor:  "+cursor.getCount());
		
		if(cursor.moveToFirst()){
		
		Log.d(ASIMOV,String.valueOf(ID)+"***/////////////////*"+cursor.getString(0).toString());
		
		txtShowTipo= (TextView) findViewById(R.id.txtShowTipo);
		txtShowTipo.setText(cursor.getString(1));
		
		Log.d(ASIMOV,String.valueOf(ID)+"----TIPO");
		
		
		txtShowDescripcion= (TextView) findViewById(R.id.txtShowDescripcion);
		txtShowDescripcion.setText(cursor.getString(2));
		
		Log.d(ASIMOV,String.valueOf(ID)+"--Descripcion");
		
		txtShowFecha= (TextView) findViewById(R.id.txtShowFecha);
		txtShowFecha.setText(cursor.getString(3));
		
		Log.d(ASIMOV,String.valueOf(ID)+"---FECHA");
		
		txtShowDuracion= (TextView) findViewById(R.id.txtShowDuracion);
		txtShowDuracion.setText(cursor.getString(4));
		
		Log.d(ASIMOV,String.valueOf(ID)+"---DURACION");
		
		txtShowDistancia= (TextView) findViewById(R.id.txtShowDistancia);
		txtShowDistancia.setText(cursor.getString(5));
		
		Log.d(ASIMOV,String.valueOf(ID)+"----DISTANCIA");
		
		txtShowVelocidad= (TextView) findViewById(R.id.txtShowVelocidad);
		txtShowVelocidad.setText(cursor.getString(6));
		
		Log.d(ASIMOV,String.valueOf(ID)+"----VELOCIDAD");
		
		txtShowOtros= (TextView) findViewById(R.id.txtShowOtros);
		txtShowOtros.setText(cursor.getString(7));
		
		Log.d(ASIMOV,String.valueOf(ID)+"----OTROS");
		
		txtShowUbicacion= (TextView) findViewById(R.id.txtShowUbicacion);
		txtShowUbicacion.setText(cursor.getString(8));
		
		Log.d(ASIMOV,String.valueOf(ID)+"----UBICACION");
		
		}
		else 
			Log.d(ASIMOV,"ERROR CURSOR-------");
		//}
		
	}
}
